﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
namespace QuanLyKhachSan
{
    public partial class UC_Phong_CaiDatGia : System.Web.UI.UserControl
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>        
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                Phong_Load();
                Gio_Phong_Load();
                Nguoi_Phong_Load();
            }
        }
        #region Add New Row
        protected void cmdAdd_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                ImageButton cmdAdd = (ImageButton)sender;
                List<Gio_PhongInfo> ListGioPhong = new List<Gio_PhongInfo>();
                Gio_PhongInfo GioPhong = null;
                //duyet lstGiaTheoGio | txtGioPhongName | txtGia
                foreach (DataListItem dli in lstGiaTheoGio.Items)
                {
                    if (dli.ItemType == ListItemType.Item || dli.ItemType == ListItemType.AlternatingItem)
                    {
                        TextBox txtGioPhongName = dli.FindControl("txtGioPhongName") as TextBox;
                        TextBox txtGia = dli.FindControl("txtGia") as TextBox;
                        TextBox txtGio_PhongID = dli.FindControl("txtGio_PhongID") as TextBox;
                        Image cmdAdd_Row = dli.FindControl("cmdAdd") as Image;
                        GioPhong = new Gio_PhongInfo();
                        GioPhong.Gio_PhongID = int.Parse(txtGio_PhongID.Text.ToString());
                        GioPhong.GioPhongName = txtGioPhongName.Text;
                        GioPhong.ImageUrl = cmdAdd_Row.ImageUrl;
                        GioPhong.GiaTien = decimal.Parse(txtGia.Text.ToString());
                        ListGioPhong.Add(GioPhong);
                    }
                }
                //add new row
                int Gio_PhongID = 0;
                if (cmdAdd.ImageUrl.Contains("add.png"))
                {
                    if (Session["Gio_PhongID_Next"] != null)
                    {
                        Gio_PhongID = int.Parse(Session["Gio_PhongID_Next"].ToString());
                    }
                    GioPhong = new Gio_PhongInfo();
                    GioPhong.Gio_PhongID = Gio_PhongID;
                    GioPhong.GiaTien = 0;
                    GioPhong.GioPhongName = "";
                    GioPhong.ImageUrl = "~/Images/delete.png";
                    ListGioPhong.Add(GioPhong);
                }
                else
                {
                    Gio_PhongID = int.Parse(cmdAdd.CommandArgument.ToString());
                    for (int i = 0; i < ListGioPhong.Count; i++)
                    {
                        if (ListGioPhong[i].Gio_PhongID == Gio_PhongID)
                        {
                            ListGioPhong.RemoveAt(i);
                            break;
                        }
                    }
                }
                Session["Gio_PhongID_Next"] = Gio_PhongID + 1;
                lstGiaTheoGio.DataSource = ListGioPhong;
                lstGiaTheoGio.DataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void cmdAddNguoiPhong_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                ImageButton cmdAddNguoiPhong = (ImageButton)sender;
                List<Nguoi_PhongInfo> ListNguoiPhong = new List<Nguoi_PhongInfo>();
                Nguoi_PhongInfo NguoiPhong = null;
                //duyet lstGiaTheoGio | txtGioPhongName | txtGia
                foreach (DataListItem dli in lstNguoiPhong.Items)
                {
                    if (dli.ItemType == ListItemType.Item || dli.ItemType == ListItemType.AlternatingItem)
                    {
                        TextBox txtThem = dli.FindControl("txtThem") as TextBox;
                        TextBox txtCong = dli.FindControl("txtCong") as TextBox;
                        TextBox txtNguoi_PhongID = dli.FindControl("txtNguoi_PhongID") as TextBox;
                        Image cmdAddNguoiPhong_Row = dli.FindControl("cmdAddNguoiPhong") as Image;
                        NguoiPhong = new Nguoi_PhongInfo();
                        NguoiPhong.Nguoi_PhongID = int.Parse(txtNguoi_PhongID.Text.ToString());
                        NguoiPhong.NguoiPhongName = txtThem.Text;
                        NguoiPhong.CongThem = txtCong.Text.ToString();
                        NguoiPhong.ImageUrl = cmdAddNguoiPhong_Row.ImageUrl;
                        ListNguoiPhong.Add(NguoiPhong);
                    }
                }
                //add new row
                int Nguoi_PhongID = 0;
                if (cmdAddNguoiPhong.ImageUrl.Contains("add.png"))
                {
                    if (Session["Nguoi_PhongID_Next"] != null)
                    {
                        Nguoi_PhongID = int.Parse(Session["Nguoi_PhongID_Next"].ToString());
                    }
                    NguoiPhong = new Nguoi_PhongInfo();
                    NguoiPhong.Nguoi_PhongID = Nguoi_PhongID;
                    NguoiPhong.NguoiPhongName = "";
                    NguoiPhong.CongThem = "";
                    NguoiPhong.ImageUrl = "~/Images/delete.png";
                    ListNguoiPhong.Add(NguoiPhong);
                }
                else
                {
                    Nguoi_PhongID = int.Parse(cmdAddNguoiPhong.CommandArgument.ToString());
                    for (int i = 0; i < ListNguoiPhong.Count; i++)
                    {
                        if (ListNguoiPhong[i].Nguoi_PhongID == Nguoi_PhongID)
                        {
                            ListNguoiPhong.RemoveAt(i);
                            break;
                        }
                    }
                }
                Session["Nguoi_PhongID_Next"] = Nguoi_PhongID + 1;
                lstNguoiPhong.DataSource = ListNguoiPhong;
                lstNguoiPhong.DataBind();
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }

        #endregion
        /// <summary>
        /// 
        /// </summary>
        protected void Phong_Load()
        {
            try
            {
                dlPhong.DataSource = PhongBLL.Phong_GetItems(0);
                dlPhong.DataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        protected void Nguoi_Phong_Load()
        {
            try
            {
                //Tao Du Lieu Trong
                List<Nguoi_PhongInfo> GioPhong = new List<Nguoi_PhongInfo>();
                Nguoi_PhongInfo Item = new Nguoi_PhongInfo();
                Item.Nguoi_PhongID = 1;
                Item.GiaTien = 0;
                Item.NguoiPhongName = "1";
                Item.GiaTien = 0;
                Item.PhanTram = 0;
                Item.ImageUrl = "~/Images/add.png";
                GioPhong.Add(Item);
                Session["Nguoi_PhongID_Next"] = 2;
                lstNguoiPhong.DataSource = GioPhong;
                lstNguoiPhong.DataBind();
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        protected void Gio_Phong_Load()
        {
            try
            {
                //Tao Du Lieu Trong
                List<Gio_PhongInfo> GioPhong = new List<Gio_PhongInfo>();
                Gio_PhongInfo Item = new Gio_PhongInfo();
                Item.Gio_PhongID = 1;
                Item.GiaTien = 0;
                Item.GioPhongName = "1";
                Item.GiaTien = 0;
                Item.PhanTram = 0;
                Item.ImageUrl = "~/Images/add.png";
                GioPhong.Add(Item);
                //Luu Session 
                Session["Gio_PhongID_Next"] = 2;
                lstGiaTheoGio.DataSource = GioPhong;
                lstGiaTheoGio.DataBind();
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }
        public string FormatMoney(string GiaTien)
        {
            return String.Format("{0:C}", GiaTien);
        }
        public string Format_NumberVietnamese(string Gia)
        {
            try
            {
                if (Gia.IndexOf("%") > 0 || Gia == "")
                {
                    return Gia;
                }
                else
                {
                    decimal value = decimal.Parse(Gia);
                    string retGia = value.ToString("N", CultureInfo.CreateSpecificCulture("en-US"));
                    return retGia.Replace(".00", "");
                }

            }
            catch (Exception)
            {
                throw;
            }
        }
        public void Get_Data(int PhongID)
        {
            try
            {
                #region get Gio_Phong
                List<Gio_PhongInfo> GioPhong = new List<Gio_PhongInfo>();
                List<Gio_PhongInfo> GioPhongNew = new List<Gio_PhongInfo>();
                Gio_PhongInfo GioPhongItem = null;

                GioPhong = Gio_PhongBLL.Gio_Phong_GetItemByPhongID(PhongID);
                if (GioPhong.Count > 0)
                {
                    for (int i = 0; i < GioPhong.Count; i++)
                    {
                        GioPhongItem = new Gio_PhongInfo();
                        GioPhongItem.Gio_PhongID = GioPhong[i].Gio_PhongID;
                        GioPhongItem.GiaTien = GioPhong[i].GiaTien;
                        GioPhongItem.GioPhongName = GioPhong[i].GioPhongName;
                        GioPhongItem.GiaTien = GioPhong[i].GiaTien;
                        GioPhongItem.PhanTram = GioPhong[i].PhanTram;
                        if (i == 0)
                            GioPhongItem.ImageUrl = "~/Images/add.png";
                        else
                            GioPhongItem.ImageUrl = "~/Images/delete.png";

                        GioPhongNew.Add(GioPhongItem);
                    }
                    lstGiaTheoGio.DataSource = GioPhongNew;
                    lstGiaTheoGio.DataBind();
                }
                else
                    Gio_Phong_Load();
                #endregion

                #region get Ngay_Phong
                Ngay_PhongInfo Item = Ngay_PhongBLL.Ngay_Phong_GetItemByPhongID(PhongID);
                txtTheoNgay.Text = Format_NumberVietnamese(Item.GiaNgay.ToString());
                txtTheoTuan.Text = Format_NumberVietnamese(Item.GiaTuan.ToString());
                txtTheoThang.Text = Format_NumberVietnamese(Item.GiaThang.ToString());
                #endregion

                #region get Nguoi_Phong
                List<Nguoi_PhongInfo> NguoiPhong = new List<Nguoi_PhongInfo>();
                List<Nguoi_PhongInfo> NguoiPhongNew = new List<Nguoi_PhongInfo>();
                Nguoi_PhongInfo NguoiPhongItem = null;
                
                NguoiPhong = Nguoi_PhongBLL.Nguoi_Phong_GetItemByPhongID(PhongID);
                if (NguoiPhong.Count > 0)
                {
                    foreach (Nguoi_PhongInfo item in NguoiPhong)
                    {
                        if (item.status.Trim().Equals("GiaCuoi"))
                            txtCongThem.Text = item.CongThem;
                        else
                        {
                            NguoiPhongItem = new Nguoi_PhongInfo();
                            NguoiPhongItem.Nguoi_PhongID = item.Nguoi_PhongID;
                            NguoiPhongItem.PhongID = item.PhongID;
                            NguoiPhongItem.NguoiPhongName = item.NguoiPhongName;
                            NguoiPhongItem.CongThem = item.CongThem;
                            NguoiPhongItem.ImageUrl = "~/Images/add.png";
                            NguoiPhongNew.Add(NguoiPhongItem);
                        }
                    }
                    lstNguoiPhong.DataSource = NguoiPhongNew;
                    lstNguoiPhong.DataBind();
                }
                else
                    Gio_Phong_Load();
                #endregion
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }
        #region Format
        public string PhanTram(string _phantram)
        {
            if (_phantram == "" || _phantram == "0")
                return "---";
            else
                return "[" + _phantram + " %]";
        }
        #endregion
        protected void cmdCapNhat_Click(object sender, EventArgs e)
        {
            try
            {
                //Gio_Phong_Add
                string ListPhongID = "";
                string ListGioPhongName = "";
                int PhanTram = 0;
                string ListGiaTien = "";
                string Status = "";
                //ListPhongID
                foreach (DataListItem dli in dlPhong.Items)
                {
                    if (dli.ItemType == ListItemType.Item || dli.ItemType == ListItemType.AlternatingItem)
                    {
                        CheckBox cbxCheck = dli.FindControl("cbxCheck") as CheckBox;
                        HiddenField hfPhongID = dli.FindControl("hfPhongID") as HiddenField;
                        if (cbxCheck.Checked)
                        {
                            ListPhongID += hfPhongID.Value + ";";
                        }
                    }
                }
                //ListGioPhongName
                foreach (DataListItem dli in lstGiaTheoGio.Items)
                {
                    if (dli.ItemType == ListItemType.Item || dli.ItemType == ListItemType.AlternatingItem)
                    {
                        TextBox txtGioPhongName = dli.FindControl("txtGioPhongName") as TextBox;
                        TextBox txtGia = dli.FindControl("txtGia") as TextBox;
                        if (txtGioPhongName.Text != "")
                        {
                            ListGioPhongName += txtGioPhongName.Text + ";";
                            ListGiaTien += txtGia.Text + ";";
                        }
                    }
                }
                Gio_PhongBLL.Gio_Phong_Add(ListPhongID, ListGioPhongName, PhanTram, ListGiaTien, Status);
                //Ngay_Phong_Add
                decimal GiaNgay = decimal.Parse(txtTheoNgay.Text.ToString() == "" ? "0" : txtTheoNgay.Text.ToString());
                decimal GiaTuan = decimal.Parse(txtTheoTuan.Text.ToString() == "" ? "0" : txtTheoTuan.Text.ToString());
                decimal GiaThang = decimal.Parse(txtTheoThang.Text.ToString() == "" ? "0" : txtTheoThang.Text.ToString());
                Ngay_PhongBLL.Ngay_Phong_Add(ListPhongID, GiaNgay, GiaTuan, GiaThang);
                //Nguoi_Phong_Add
                string ListNguoiPhongName = "";
                string ListCongThem = "";
                string CongThemTiepTheo = txtCongThem.Text == "" ? "0%" : txtCongThem.Text;
                foreach (DataListItem dli in lstNguoiPhong.Items)
                {
                    if (dli.ItemType == ListItemType.Item || dli.ItemType == ListItemType.AlternatingItem)
                    {
                        TextBox txtThem = dli.FindControl("txtThem") as TextBox;
                        TextBox txtCong = dli.FindControl("txtCong") as TextBox;
                        if (txtThem.Text != "")
                        {
                            ListNguoiPhongName += txtThem.Text + ";";
                            ListCongThem += txtCong.Text + ";";
                        }
                    }
                }
                Nguoi_PhongBLL.Nguoi_Phong_Add(ListPhongID, ListNguoiPhongName, ListCongThem, CongThemTiepTheo);

            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }
        protected void cmdXem_Click(object sender, EventArgs e)
        {
            try
            {
                
                cmdCapNhat.Visible = false;
                LinkButton cmdXem = (LinkButton)sender;
                string PhongID = cmdXem.CommandArgument;
                //checked checkbox
                foreach (DataListItem dli in dlPhong.Items)
                {
                    if (dli.ItemType == ListItemType.Item || dli.ItemType == ListItemType.AlternatingItem)
                    {
                        CheckBox cbxCheck = dli.FindControl("cbxCheck") as CheckBox;
                        HiddenField hfPhongID = dli.FindControl("hfPhongID") as HiddenField;
                        if (hfPhongID.Value == PhongID)
                        {
                            cbxCheck.Checked = true;
                            Get_Data(int.Parse(PhongID));
                        }
                        else
                        {
                            cbxCheck.Checked = false;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }
        protected void cmdSua_Click(object sender, EventArgs e)
        {
            try
            {
                List<Gio_PhongInfo> GioPhong = new List<Gio_PhongInfo>();
                cmdCapNhat.Visible = true;
                LinkButton cmdSua = (LinkButton)sender;
                string PhongID = cmdSua.CommandArgument;
                //checked checkbox
                foreach (DataListItem dli in dlPhong.Items)
                {
                    if (dli.ItemType == ListItemType.Item || dli.ItemType == ListItemType.AlternatingItem)
                    {
                        CheckBox cbxCheck = dli.FindControl("cbxCheck") as CheckBox;
                        HiddenField hfPhongID = dli.FindControl("hfPhongID") as HiddenField;
                        if (hfPhongID.Value == PhongID)
                        {
                            cbxCheck.Checked = true;
                            Get_Data(int.Parse(PhongID));
                        }
                        else
                        {
                            cbxCheck.Checked = false;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }
    }
}

